﻿using System;
using System.Data.SqlClient;
using System.Data;

namespace GetMesService
{
    public class SqlDBClass
    {
        /*----全局变量定义------*/

        private SqlConnection conn;
        private SqlCommand comm;
        private string connStr;   /* 数据库联接字符串 */
        public string errInfo = "";
        //static log4net.ILog _logEik = log4net.LogManager.GetLogger(typeof(BLLUltimusEik));
        /*----------------------*/

        public SqlDBClass()
        {
            connStr = "";
        }
        public void GetConString(string ConnString)
        {
            connStr = ConnString;
        }
        public SqlDBClass(string ConnString)
        {
            connStr = ConnString;
        }
        /* 数据库操作异常信息 只读属性 */
        public string ErrInfo
        {
            get
            {
                return errInfo;
            }
        }

        /* 数据库链接字符串 */
        public string ConnStr
        {
            get
            {
                return connStr;
            }
            set
            {
                connStr = value;
            }
        }
        private void connDb()
        {
            try
            {
                if (conn == null || conn.State == ConnectionState.Closed)
                {
                    conn = new SqlConnection(connStr);
                    conn.Open();
                }
            }
            catch (Exception e)
            {
                errInfo = e.Message;
                conn.Close();
            }
        }

        /* 数据库链接字符串 */
        public SqlConnection Connection
        {
            get
            {
                return conn;
            }
            set
            {
                conn = value;
            }
        }

        public SqlConnection getConnect()
        {
            SqlConnection Connection = null;
            try
            {
                if (conn == null || conn.State == ConnectionState.Closed)
                {
                    conn = new SqlConnection(connStr);
                    conn.Open();

                    Connection = conn;
                }
            }
            catch (Exception e)
            {
                errInfo = e.Message;
                conn.Close();

            }

            return Connection;
        }

        /* 获取DataSet */
        public DataSet GetDataSet(string sql)
        {
            connDb();
            SqlDataAdapter dad = new SqlDataAdapter(sql, conn);
            DataSet ds = new DataSet();
            dad.Fill(ds);
            this.clear();
            return ds;
        }

        public DataSet GetDataSet(string sql, SqlConnection Connection)
        {
            conn = Connection;
            SqlDataAdapter dad = new SqlDataAdapter(sql, conn);
            DataSet ds = new DataSet();
            dad.Fill(ds);
            return ds;
        }

        /* 获取DataTable */
        public DataTable GetDataTable(string sql)
        {

            connDb();
            SqlDataAdapter dad = new SqlDataAdapter(sql, conn);
            //_logEik.Error("sql=" + sql);
            DataSet ds = new DataSet();
            dad.Fill(ds);

            if (ds != null)
            {
                if (ds.Tables.Count > 0)
                {
                    this.clear();
                    return ds.Tables[0];
                }
                else
                {
                    this.clear();
                    return null;
                }
            }
            else
            {
                this.clear();
                return null;
            }
        }

        public DataTable GetDataTable(string sql, SqlConnection Connection)
        {
            conn = Connection;

            SqlDataAdapter dad = new SqlDataAdapter(sql, conn);
            DataSet ds = new DataSet();
            dad.Fill(ds);

            if (ds != null)
            {
                if (ds.Tables.Count > 0)
                    return ds.Tables[0];
                else
                    return null;
            }
            else
                return null;
        }

        /* 执行SQL语句，不返回结果 */
        public int exeSql(string sql)
        {
            try
            {
                connDb();
                comm = new SqlCommand(sql, conn);
                int pReturn = comm.ExecuteNonQuery();
                this.clear();
                return pReturn;
            }
            catch (Exception e)
            {
                errInfo = e.Message;
                this.clear();
                return 0;
            }
        }

        public int exeSql(string sql, SqlConnection Connection)
        {
            try
            {
                conn = Connection;
                comm = new SqlCommand(sql, conn);
                int pReturn = comm.ExecuteNonQuery();
                return pReturn;
            }
            catch (Exception e)
            {
                errInfo = e.Message;
                this.clear();
                return 0;
            }
        }
        /* 关闭链接 */
        public void clear()
        {
            if (conn.State == ConnectionState.Open)
            {
                conn.Close();
            }
        }
    }
}
